package com.magicsoft.share.ui.my;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.magicsoft.share.R;
import com.magicsoft.share.adapter.my.FollowAdapter;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.my.MessageList;
import com.magicsoft.share.presenter.my.MessageListPresenter;
import com.magicsoft.share.ui.share.HomePageActivity;
import com.magicsoft.share.utils.DensityUtils;
import com.magicsoft.share.view.my.IMessageListView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Administrator
 *         <p>
 *         关注消息
 */
public class FollowMsgActivity extends BaseActivity<IMessageListView, MessageListPresenter> implements IMessageListView {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView recyclerView;
    private FollowAdapter followAdapter;


    private int pageNo = 1;
    private int pageSize = 10;
    private boolean isRefresh = true;

    @Override
    public MessageListPresenter initPresenter() {
        return new MessageListPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_follow_msg;
    }

    @Override
    public void initData() {
        setResult(RESULT_OK);
    }

    @Override
    public void initView() {
        tvTitle.setText("关注");
        initRV();
    }

    @Override
    public void requestData() {
        mPresenter.messageList( 3, pageNo, pageSize);
    }


    @OnClick(R.id.ibtn_left)
    public void onViewClicked() {
        finish();
    }

    private void initRV() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.bg), 0, DensityUtils.dp2px(this, 0.5f)));       /* recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));*/


        recyclerView.useDefaultLoadMore();
        recyclerView.loadMoreFinish(true, false);
        recyclerView.setLoadMoreListener(new SwipeMenuRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {

                isRefresh = false;
                pageNo++;
                requestData();
            }
        });

        followAdapter = new FollowAdapter(R.layout.item_msg_follow);
        recyclerView.setAdapter(followAdapter);
        followAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                 startActivity(new Intent(FollowMsgActivity.this, HomePageActivity.class)
                 .putExtra("appUserId",followAdapter.getItem(position).getFromId())
                 );
            }
        });

    }

    @Override
    public void messageListSuccess(MessageList data) {
        if (isRefresh) {
            followAdapter.setNewData(data.getList());
        } else {
            followAdapter.addData(data.getList());
        }

        if (pageNo < data.getPages()) {
            recyclerView.loadMoreFinish(false, true);
        } else if (pageNo == data.getPages()) {
            recyclerView.loadMoreFinish(false, false);
        } else {
            recyclerView.loadMoreFinish(true, false);
        }
    }

    @Override
    public void messageListFail() {
        if (isRefresh) {
            recyclerView.loadMoreFinish(true, false);
        } else {
            recyclerView.loadMoreFinish(false, false);
        }
    }
}
